package com.cloudcross.ssp.common.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HttpClienUtil {

    private static CloseableHttpClient httpClient = HttpClients.createDefault();

    private static Logger logger = LogManager.getLogger(HttpClienUtil.class);

    /**
     * @param url
     *            请求url
     * @param reqParams
     *            参数
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String doGet(String url, Map<String, String> reqParams) {
        CloseableHttpResponse response = null;
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        String param = "";
        if (null != reqParams && !reqParams.isEmpty()) {
            for (Entry<String, String> entry : reqParams.entrySet()) {
                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        try {
            param = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
            HttpGet httpGet = new HttpGet(url + "?" + param);
            httpGet.addHeader("Connection", "keep-alive");
            response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(response.getEntity());
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("exception", e);
            return null;
        }
    }

    /**
     * post
     * 
     * @param url
     * @param json
     * @throws Exception
     */
    public static String postJson(String url, String json) {
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Connection", "keep-alive");
            // 参数
            StringEntity se = new StringEntity(json);
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json;charset=utf-8");// 发送json需要设置contentType
            httpPost.setEntity(se);
            response = httpClient.execute(httpPost);
            // 解析返结果
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String resStr = EntityUtils.toString(entity, "UTF-8");
                return resStr;
            }
            return null;
        } catch (Exception e) {
            logger.error("exception", e);
            return null;
        }
    }
}
